<%@page import="com.wowangz.cms.common.utils.MessageObject"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%-- 引入页面头 --%>
<%@ include file="/pageHead.jsp"%>

<form name="form1" id="form1" action="" method="post">
	<div class="pageMain">
		<div class="pageTitle">
		<%=MessageResourcesUtils.getMessage(request, "systemmanage.bascsetting.title") %>
		<span></span>
		<%=MessageResourcesUtils.getMessage(request, "systemmanage.sms.title") %>
		<span></span>
		<%=MessageResourcesUtils.getMessage(request, "systemmanage.sms.edit.title") %></div>
		<div class="pageBody">
			<div class="form-group">
				<ul>
					<li class="liw">
						<i></i>
						<label>
							<%=MessageResourcesUtils.getMessage(request, "systemmanage.sms.column.templatecode") %>
						</label>
						<span>
							<input id="id" name="id" value="${smsSet.id}" type="hidden">
							<input type="text" readonly="readonly" value="${smsSet.templatecode}" id="templatecode" name="templatecode" />
						</span>
					</li>
					<li>
						<i>*</i>
						<label><%=MessageResourcesUtils.getMessage(request, "systemmanage.sms.column.templatetype") %></label>
						<span>
							<select id="templatetype" name="templatetype" class="validate[required]"></select>
						</span>
					</li>
				</ul>
				<ul>
					<li class="liw">
						<i></i>
						<label>
							<!-- 短信参数 -->
							<%=MessageResourcesUtils.getMessage(request, "systemmanage.sms.column.smsparameter") %>
						</label>
						<span class="smsparameter">
							
						</span>
					</li>
				</ul>
				<ul>
					<li class="liw">
						<i>*</i>
						<label><%=MessageResourcesUtils.getMessage(request, "systemmanage.sms.column.tamplatecontent") %></label>
						<span>
							<textarea class="validate[required,maxSize[200]]" rows="10" cols="70"  name="templatecontent" id="templatecontent">${smsSet.templatecontent}</textarea>
						</span>
					</li>
				</ul>	
			</div>			
        	<div class="button-group">
       			<input type="hidden"  name="updateposition" id="updateposition" value="0"/>
       			<input type="button" class="btn btn-default"  id="butSave" name="butSave" value='<%=MessageResourcesUtils.getMessage(request, "common.button.save") %>' />
       			<input type="button" class="btn btn-default"  id="butReturn" name="butReturn" value='<%=MessageResourcesUtils.getMessage(request, "common.button.back") %>' />
			</div>
		</div>
	</div>
</form>

<script type="text/javascript">	
	(function($) {
		$.fn.extend({
			insertAtCaret : function(myValue) {
				var $t = $(this)[0];
				if (document.selection) {
					this.focus();
					sel = document.selection.createRange();
					sel.text = myValue;
					this.focus();
				} else if ($t.selectionStart || $t.selectionStart == '0') {
					var startPos = $t.selectionStart;
					var endPos = $t.selectionEnd;
					var scrollTop = $t.scrollTop;
					$t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
					this.focus();
					$t.selectionStart = startPos + myValue.length;
					$t.selectionEnd = startPos + myValue.length;
					$t.scrollTop = scrollTop;
				} else {
					this.value += myValue;
					this.focus();
				}
			}
		})
	})(jQuery);

	$(document).ready(function() {
		$("#templatetype").selectutil({
			optModel :<%=SysConstant.Templatetype.getModelJSONString(true, false)%>,
			optDefault :'${smsSet.templatetype}'
		});
		$('#butSave').click(function() {
			var exp = $('#form1').validationEngine('validate');
			if(exp==true) {
				// 显示进度条
				showSending();
				$.ajax({
					type: 'POST',
				    url: '${systemctx}/systemmanage/sms/smsSet/smsSetUpdate.json',
				    data: $("#form1").serialize(),
				    dataType: 'json',
				    success: function(data) {
				    	//关闭执行...
						closeSending();
						if(data.messagetype == 1){
							alert(data.message);
							parent.document.location.reload();
	    					$.fn.closeIssWindow();
						}else {
							alert(data.message);
							return;							
						}
				    }
				});
			}
		});
	
		var type = '${smsSet.templatetype}';
		if(type != ''){
			$.getJSON('${systemctx}/systemmanage/sms/smsSet/getTemplateParamerMap.json?code=' + type,function(data){
				if(data.resposecode == '<%=MessageObject.ResponseCode.code_200%>') {
					var objs = data.object;
					var button = "";
					for(var i = 0;i < objs.length; i ++){
						var obj = objs[i];
						button += '<button type="button" value="'+obj.key+'">' +obj.value+ '</button>&nbsp;&nbsp;';
					}
					$(".smsparameter").html(button);
					bindEvent();
				} else {
					alert(data.message);
					return ;
				}
			});
		}
		$("#templatetype").on('change',function(){
			var select_val = $(this).children('option:selected').val();
			if(select_val != '' && select_val != undefined){
				$.getJSON('${systemctx}/systemmanage/sms/smsSet/getTemplateParamerMap.json?code=' + select_val,function(data){
					if(data.resposecode == '<%=MessageObject.ResponseCode.code_200%>') {
						var objs = data.object;
						var button = "";
						for(var i = 0;i < objs.length; i ++){
							var obj = objs[i];
							button += '<button type="button" value="'+obj.key+'">' +obj.value+ '</button>&nbsp;&nbsp;';
						}
						$(".smsparameter").html(button);
						bindEvent();
					} else {
						alert(data.message);
						return ;
					}
				});
			} else {
				$(".smsparameter").html('');
			}
		});
		
		$('#butReturn').click(function(){
			parent.document.location.reload();
		    $.fn.closeIssWindow();
		});
		
	});
	
	function bindEvent(){
		$(".smsparameter :button").on('click',function(){
			$("#templatecontent").insertAtCaret($(this).val());
		});
	}
</script>
<%-- 引入页面尾 --%>
<%@ include file="/pageFloor.jsp"%>